﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_Admin_Admin_XemLoHangDoiTac : System.Web.UI.Page
{
    int RowPerPage;
    int startIndex;
    int page;
    GlobalFunctions fc;
    int iddoitac;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["page"] != null)
            page = int.Parse(Request["page"]);
        else
            page = 0;
        RowPerPage = int.Parse(ConfigurationSettings.AppSettings["RowPerPage"]);
        startIndex = (page * RowPerPage);
        if (Request.QueryString.Get("Id") != null)
        {
            iddoitac = int.Parse(Request.QueryString.Get("Id"));
            Session["iddoitac"] = iddoitac;
        }
        else
        {
            if (Session["iddoitac"] != null)
                iddoitac = int.Parse(Session["iddoitac"].ToString());
        }
        fc = new GlobalFunctions();
        int trangthai = 0;
        if (Session["TrangThai"] != null)
            trangthai = int.Parse(Session["TrangThai"].ToString());
        if (!this.IsPostBack)
            ddlTrangThai.SelectedValue = trangthai.ToString();

        DataDSTableAdapters.LOHANGTableAdapter lhadapter = new DataDSTableAdapters.LOHANGTableAdapter();
        PageInterface1.currentPageName = GlobalFunctions.GetCurrentPageName();
        PageInterface1.currentPageNumber = page;
        PageInterface1.totalNumberOfPage = (int)lhadapter.LOHANG_LayTongSoTheoTrangThai(trangthai) / RowPerPage;
        if ((int)lhadapter.LOHANG_LayTongSoTheoTrangThai(trangthai) % RowPerPage != 0)
            PageInterface1.totalNumberOfPage++;
        fc = new GlobalFunctions();
        DataDS.LOHANGDataTable lhtable= lhadapter.LOHANG_LayDanhSachTheoTrangThai(iddoitac, trangthai, startIndex, RowPerPage);
        string chuoi = "";
        chuoi += "<table border='1' cellspacing='0'>";
        chuoi += "<tr class='rowheader'>";
        chuoi += "<th>Mã SP</th>";
        chuoi += "<th>Tên SP</th>";
        chuoi += "<th>TTCT</th>";
        chuoi += "<th>Hạn SD</th>";
        chuoi += "<th>Thời Gian Nhận Service</th>";
        chuoi += "<th>Số Lượng</th>";
        chuoi += "<th>Giá Cả</th>";
        chuoi += "<th>Chiết Khấu(%)</th>";
        chuoi += "<th>Trạng Thái</th>";
        chuoi += "<th>Cập Nhật</th>";
        chuoi += "<th>Gửi Cho NCU</th>";
        chuoi += "</tr>";
        for (int i = 0; i < lhtable.Rows.Count; i++)
        {
            int id = (int)lhtable.Rows[i]["Id"];
            string TenSP = lhtable.Rows[i]["TenSP"].ToString();
            string MaSPLH = lhtable.Rows[i]["MaSPLH"].ToString();
            string ThongTinChiTiet = lhtable.Rows[i]["ThongTinChiTiet"].ToString();
            DateTime HanSuDung =(DateTime) lhtable.Rows[i]["HanSuDung"];
            DateTime ThoiGianDang = (DateTime)lhtable.Rows[i]["ThoiGianDang"];
            string SoLuong = lhtable.Rows[i]["SoLuong"].ToString();
            string DonGia = lhtable.Rows[i]["DonGia"].ToString();
            string ChietKhau = lhtable.Rows[i]["ChietKhau"].ToString();
            int TrangThai = (int)lhtable.Rows[i]["TrangThai"];
            if(i%2==0)
                chuoi += "<tr class='rowchan'>";
            else
                chuoi += "<tr class='rowle'>";
            chuoi += "<td>" +MaSPLH + "</td>";
            chuoi += "<td>" + TenSP + "</td>";
            chuoi += "<td>" + ThongTinChiTiet + "</td>";
            chuoi += "<td>" + HanSuDung.ToString("d-M-yyyy") + "</td>";
            chuoi += "<td>" + ThoiGianDang.ToString("d-M-yyyy") + "</td>";
            if (TrangThai == 1)
            {
                chuoi += "<td><input type='text' size='2' name='" + id.ToString() + "txtSoLuong' value='" + SoLuong + "' /></td>";
                chuoi += "<td><input type='text' size='6' name='" + id.ToString() + "txtDonGia' value='" + DonGia + "' /></td>";
                chuoi += "<td><input type='text' size='2' name='" + id.ToString() + "txtChietKhau' value='" + ChietKhau + "' /></td>";
            }
            else
            {
                chuoi += "<td>" + SoLuong +"</td>";
                chuoi += "<td>" + DonGia + "</td>";
                chuoi += "<td>" + ChietKhau + "</td>";
            }
            chuoi += "<td><select name='"+id.ToString() +"cmbTrangThai'>";
            if (TrangThai == 1)
                chuoi += "<option value='1' selected >Đang Thỏa Thuận</option>";
            else
                chuoi += "<option value='1'>Đang Thỏa Thuận</option>";
            if (TrangThai != 1)
            {
                if (TrangThai <= 2)
                {
                    if (TrangThai == 2)
                        chuoi += "<option value='2' selected >Không Nhận</option>";
                    else
                        chuoi += "<option value='2'>Không Nhận</option>";
                }
                if (TrangThai <= 3)
                {
                    if (TrangThai == 3)
                        chuoi += "<option value='3' selected >Nhận,Đang Chờ Giao</option>";
                    else
                        chuoi += "<option value='3'>Nhận,Đang Chờ Giao</option>";
                }
                if (TrangThai <= 4)
                {
                    if (TrangThai == 4)
                        chuoi += "<option value='4' selected >Giao Xong</option>";
                    else
                        chuoi += "<option value='4'>Giao Xong</option>";
                }
                if (TrangThai <= 5)
                {
                    if (TrangThai == 5)
                        chuoi += "<option value='5' selected >Hủy Hợp Đồng</option>";
                    else
                        chuoi += "<option value='5'>Hủy Hợp Đồng</option>";
                }
            }

            chuoi += "</select></td>";
            if(TrangThai==4)
                chuoi += "<td></td>";
            else
                chuoi += "<td><input type='checkbox' name='chkCapNhat' value='"+ id.ToString()+"' /></td>";

            if(TrangThai==1)
                chuoi += "<td><input type='checkbox' name='chkGui' value='" + id.ToString() + "' /></td>";
            else
                chuoi += "<td></td>";
            chuoi += "</tr>";
        }
        chuoi += "</table>";
        lblLoHang.Text = chuoi;
    }
    protected void ddlTrangThai_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["TrangThai"] = ddlTrangThai.SelectedValue;
        string s = GlobalFunctions.GetCurrentPageName();
        Response.Redirect(Request.UrlReferrer.ToString());
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        DataDSTableAdapters.LOHANGTableAdapter lhadapter = new DataDSTableAdapters.LOHANGTableAdapter();
        DataDSTableAdapters.DOITACTableAdapter dtadapter = new DataDSTableAdapters.DOITACTableAdapter();
        string[] arrId = Request.Form.GetValues("chkCapNhat");
        if (arrId != null)
        {
            for (int i = 0; i < arrId.Length; i++)
            {
                string strsoluong = Request.Form.Get(arrId[i] + "txtSoLuong");
                string strdongia = Request.Form.Get(arrId[i] + "txtDonGia");
                string strchietkhau = Request.Form.Get(arrId[i] + "txtChietKhau");
                string strtrangthai = Request.Form.Get(arrId[i] + "cmbTrangThai");
                int soluong, chietkhau;
                float dongia;
                if (strsoluong != null)
                {
                    if (int.TryParse(strsoluong, out soluong) == false || soluong <= 0)
                        continue;
                    if (float.TryParse(strdongia, out dongia) == false || dongia <= 0)
                        continue;
                    if (int.TryParse(strchietkhau, out chietkhau) == false || chietkhau < 0)
                        continue;
                }
                else
                {
                    DataDS.LOHANGDataTable lhtable = lhadapter.LOHANG_LayTheoId(int.Parse(arrId[i]));
                    soluong = (int)lhtable.Rows[0]["SoLuong"];
                    dongia = float.Parse(lhtable.Rows[0]["DonGia"].ToString());
                    chietkhau = (int)lhtable.Rows[0]["ChietKhau"];
                }
                lhadapter.LOHANG_CapNhat(int.Parse(arrId[i]), soluong, dongia, chietkhau, int.Parse(strtrangthai));
                if (int.Parse(strtrangthai) == 4)
                {
                    DataDS.DOITACDataTable dttable= dtadapter.DOITAC_LayDoiTacTheoId(iddoitac);
                    string taikhoandoitac = dttable.Rows[0]["MaTaiKhoan_NganHang"].ToString();
                    NganHang_WS.ThanhToanService sv = new NganHang_WS.ThanhToanService();
                    sv.ThanhToanTien("1111111111", taikhoandoitac, dongia * soluong);
                }
            }
            Response.Write("<script>alert('Đã cập nhật!');location.href='" + Request.UrlReferrer.ToString()+ "';</script>");
        }
    }
   
    protected void btnGui_Click1(object sender, EventArgs e)
    {
        string[] arrId = Request.Form.GetValues("chkGui");
        if (arrId != null)
        {
            DataDSTableAdapters.LOHANGTableAdapter lhadapter = new DataDSTableAdapters.LOHANGTableAdapter();
            for (int i = 0; i < arrId.Length; i++)
            {
                int id = int.Parse(arrId[i]);
                string strtrangthai = Request.Form.Get(arrId[i] + "cmbTrangThai");
                if (int.Parse(strtrangthai) != 1)
                    continue;
                DataDS.LOHANGDataTable lhtable = lhadapter.LOHANG_LayTheoId(id);
                int soluong = (int)lhtable.Rows[0]["SoLuong"];
                float dongia = float.Parse(lhtable.Rows[0]["DonGia"].ToString());
                int chietkhau = (int)lhtable.Rows[0]["ChietKhau"];
                string masp = lhtable.Rows[0]["MaSPLH"].ToString();
                HP_WS.LoHangService sv = new HP_WS.LoHangService();
                int kq = sv.ThuongLuong(masp, soluong, (decimal)dongia, chietkhau);
                if (kq == 0)
                    lhadapter.LOHANG_CapNhat(id, soluong, dongia, chietkhau, 2);
                else
                    lhadapter.LOHANG_CapNhat(id, soluong, dongia, chietkhau, 3);
            }
            Response.Write("<script>alert('Đã xong!');location.href='" + Request.UrlReferrer.ToString() + "';</script>");
        }
    }
}
